#include "Texture.h"
#include <conio.h>

Texture::Texture(LPDIRECT3DDEVICE9 d3ddev)
{
	/**
	Initailize class Texture members
	*/
	this->d3ddev = d3ddev;
	pTexture = NULL;

	width  = 0;
	height = 0;

	tex_ID = 0;
}

Texture::~Texture()
{
	/**
	Destroy all objects
	*/
	if( pTexture != NULL )
		pTexture->Release();

	if( tex_ID > 0 )
		tex_ID -= 1;
}

bool Texture::LoadTexture(const char *filename, int id)
{
	/**
	Load a texture (Checks to see if it failed) 
	returns back true or false

	file name can take a string
	example:

	C:/Art/example.jpg
	or
	../../Art/example.jpg
	*/
	tex_ID = id; //0 should be reserved for mainCharacter texture
	bool b = true;

	
	if( FAILED(D3DXCreateTextureFromFileEx(d3ddev,					//device
										   filename,				//file path
										   D3DX_DEFAULT_NONPOW2,	//width
										   D3DX_DEFAULT_NONPOW2,	//height
										   1,						//mipmap
										   0,						//usage
										   D3DFMT_UNKNOWN,			//format 
										   D3DPOOL_MANAGED,			//pool
										   D3DX_DEFAULT,			//filter
										   D3DX_DEFAULT,			//mipfilter 
										   0,						//colorKey
										   NULL,					//srcinfo
										   NULL,					//palette
										   &pTexture)) )			//texture object to copy info to
	{
		return 0;
	}

	D3DSURFACE_DESC desc;
	pTexture->GetLevelDesc(0, &desc);

	this->width = desc.Width;
	this->height = desc.Height;

	return 1;
}

int Texture::GetWidth()
{
	return this->width;
}

int Texture::GetHeight()
{
	return this->height;
}

void Texture::UseTexture()
{
	/**
	Use the texture.
	*/
	d3ddev->SetTexture(0, pTexture);
}

LPDIRECT3DTEXTURE9 Texture::GetTexture()
{
	/**
	Get texture data.
	*/
	return pTexture;
}

bool Texture::operator ==(const Texture &right)
{
	if( this->tex_ID == right.tex_ID )
		return 1;

	return 0;
}

bool Texture::operator !=(const Texture &right)
{
	if( this->tex_ID != right.tex_ID )
		return 1;

	return 0;
}